package czh.cocoa.ui.impl;

import czh.cocoa.foundation.NSMutableArray;
import czh.cocoa.foundation.NSObject;
import czh.cocoa.foundation.NSSelector;
import czh.cocoa.foundation.NSString;
import czh.cocoa.graphics.CGRect;
import czh.cocoa.graphics.CGSize;
import czh.cocoa.ui.UIApplication;
import czh.cocoa.ui.UIColor;
import czh.cocoa.ui.UIFont;
import czh.cocoa.ui.UIImage;
import czh.cocoa.ui.UIView;
import czh.cocoa.ui.UIWindow;

public class UIActionSheet extends UIView {

	public final static int UIActionSheetStyleDefault = 0;
	public final static int UIActionSheetStyleBlackTranslucent = 1;
	public final static int UIActionSheetStyleBlackOpaque = 2;
	
	private NSString mTitle;
	
	private UIActionSheetDelegate mDelegate;
	
	private NSString mCancelButtonTitle;
	
	private NSString mDestructiveButtonTitle;
	
	private NSMutableArray mOtherButtonTitles;
	
	private int mActionSheetStyle;
	
	public UIActionSheet() {
		
	}
	
	public UIActionSheet(NSString title, UIActionSheetDelegate delegate, NSString cancelButtonTitle,
			            NSString destructiveButtonTitle, NSString... otherButtonTitles) {
		mTitle = title;
		mDelegate = delegate;
		mCancelButtonTitle = cancelButtonTitle;
		mDestructiveButtonTitle = destructiveButtonTitle;
		for (NSString btn_title : otherButtonTitles) {
			mOtherButtonTitles.addObject(btn_title);
		}
	}
	
	protected void init() {
		super.init();
		mActionSheetStyle = UIActionSheetStyleDefault;
		mOtherButtonTitles = new NSMutableArray(3);
	}
	
	public void setTitle(NSString title) {
		mTitle = title;
	}
	
	public NSString title() {
		return mTitle;
	}
	
	public int addButtonWithTitle(NSString title) {
		mOtherButtonTitles.addObject(title);
		return mDestructiveButtonTitle != null ? mOtherButtonTitles.count() : 
			                                     mOtherButtonTitles.count() - 1;
	}
	
	public NSString buttonTitleAtIndex(int buttonIndex) {
		NSMutableArray titles = new NSMutableArray();
		if (mDestructiveButtonTitle != null) {
			titles.addObject(mDestructiveButtonTitle);
		}
		titles.addObjectsFromArray(mOtherButtonTitles);
		if (mCancelButtonTitle != null) {
			titles.addObject(mCancelButtonTitle);
		}
		return (NSString)titles.objectAtIndex(buttonIndex);
	}
	
	public void setDelegate(UIActionSheetDelegate delegate) {
		mDelegate = delegate;
	}
	
	public UIActionSheetDelegate delegate() {
		return mDelegate;
	}
	
	public void setActionSheetStyle(int style) {
		mActionSheetStyle = style;
	}
	
	public int actionSheetStyle() {
		return mActionSheetStyle;
	}
	
	public void showInView(UIView view) {
		clearAllSubviews();
		
		UIWindow window = UIApplication.sharedApplication().keyWindow();
		CGRect bounds = window.bounds();
		setFrame(bounds);
		
		UIView mask_view = new UIImageView(bounds);
		mask_view.setBackgroundColor(UIColor.blackColor());
		mask_view.setAlpha(0.5f);
		addSubview(mask_view);
		
		UIImage background;
		switch (mActionSheetStyle) {
		case UIActionSheetStyleBlackOpaque:
			background = UIImage.imageNamed(NSTR("cocoa/action_sheet_background_black_opaque")); 
			break;
		case UIActionSheetStyleBlackTranslucent:
			background = UIImage.imageNamed(NSTR("cocoa/action_sheet_background_black_translucent"));
			break;
		case UIActionSheetStyleDefault:
		default:
			background = UIImage.imageNamed(NSTR("cocoa/action_sheet_background_default"));
			break;
		}
		
		final UIImageView bgview = new UIImageView(background);
		bgview.setContentStretch(CGRectMake(0.5f, 0.5f, 0.1f, 0.1f)); 
		bgview.setUserInteractionEnabled(true);
		addSubview(bgview);
		
		float bg_width = bounds.size.width;
		float bg_height = 0; 
		float bg_min_height = DIP(background.size().height);
		float bg_max_height = bounds.size.height * 0.88f;
		
		final float padding = 10;
		
		bg_height = padding * 2;
		
		if (mTitle != null) { 
			UILabel title_label = new UILabel(); 
			title_label.setText(mTitle); 
			title_label.setFont(UIFont.systemFontOfSize(23)); 
			title_label.setTextColor(UIColor.whiteColor());
			CGSize title_size = CGSizeMake(bg_width - padding * 2, bg_max_height - bg_height - padding);
			title_size = title_label.sizeThatFits(title_size);
			title_label.setFrame(CGRectMake((bg_width - title_size.width) / 2, bg_height, 
					title_size.width, title_size.height)); 
			bgview.addSubview(title_label);
						
			bg_height += title_size.height + padding;
		}
		
		int buttonIndex = 0;
		
		if (mDestructiveButtonTitle != null) {	
			UIImage btn_bg = UIImage.imageNamed(NSTR("cocoa/action_sheet_destructive_btn"));
			UIImage btn_bg2 = UIImage.imageNamed(NSTR("cocoa/action_sheet_destructive_btn_highlighted"));
			float btn_height = DIP(btn_bg.size().height);
			
			UIButton button = new UIButton();
			button.setContentStretch(CGRectMake(0.5f, 0, 0.05f, 1)); 
			button.setTitleForState(mDestructiveButtonTitle, UIControl.UIControlStateNormal); 
			button.setTitleColorForState(UIColor.whiteColor(), UIControl.UIControlStateNormal);
			button.setBackgroundImageForState(btn_bg, UIControl.UIControlStateNormal);
			button.setBackgroundImageForState(btn_bg2, UIControl.UIControlStateHighlighted);
			
			button.titleLabel().setFont(UIFont.boldSystemFontOfSize(25));
			
			button.setFrame(CGRectMake(padding * 2, bg_height, 
					        bg_width - padding * 4, btn_height)); 
			
			button.addTargetActionForControlEvents(this, new NSSelector() {
				@Override
				public void perform(NSObject receiver, NSObject params) {
					dismissWithClickedButtonIndexAnimated(0, true);
				}
			}, UIControl.UIControlEventTouchUpInside);
			
			bgview.addSubview(button);
			
			bg_height += btn_height + padding;
			
			buttonIndex += 1;
		}
		
		int other_btn_count = mOtherButtonTitles.count();
		if (other_btn_count > 0) {
			UIImage btn_bg = UIImage.imageNamed(NSTR("cocoa/action_sheet_other_btn"));
			UIImage btn_bg2 = UIImage.imageNamed(NSTR("cocoa/action_sheet_btn_highlighted"));
			float btn_height = DIP(btn_bg.size().height);
			
			for (int i = 0; i < other_btn_count; i++) {
				NSString btn_title = (NSString) mOtherButtonTitles.objectAtIndex(i);
				UIButton button = new UIButton();
				button.setContentStretch(CGRectMake(0.5f, 0, 0.05f, 1)); 
				button.setTitleForState(btn_title, UIControl.UIControlStateNormal); 
				button.setTitleColorForState(UIColor.blackColor(), UIControl.UIControlStateNormal);
				button.setTitleColorForState(UIColor.whiteColor(), UIControl.UIControlStateHighlighted);
				button.setBackgroundImageForState(btn_bg, UIControl.UIControlStateNormal);
				button.setBackgroundImageForState(btn_bg2, UIControl.UIControlStateHighlighted);
				
				button.titleLabel().setFont(UIFont.boldSystemFontOfSize(25));
				
				button.setFrame(CGRectMake(padding * 2, bg_height, 
						        bg_width - padding * 4, btn_height)); 
				
				final int index = buttonIndex;
				button.addTargetActionForControlEvents(this, new NSSelector() {
					@Override
					public void perform(NSObject receiver, NSObject params) {
						dismissWithClickedButtonIndexAnimated(index, true);
					}
				}, UIControl.UIControlEventTouchUpInside);
				
				bgview.addSubview(button);
				
				bg_height += btn_height + padding;
				
				buttonIndex += 1;
			}
		}
		
		if (mCancelButtonTitle != null) {	
			UIImage btn_bg = UIImage.imageNamed(NSTR("cocoa/action_sheet_cancel_btn"));
			UIImage btn_bg2 = UIImage.imageNamed(NSTR("cocoa/action_sheet_btn_highlighted"));
			float btn_height = DIP(btn_bg.size().height);
			
			UIButton button = new UIButton();
			button.setContentStretch(CGRectMake(0.5f, 0, 0.05f, 1)); 
			button.setTitleForState(mCancelButtonTitle, UIControl.UIControlStateNormal); 
			button.setTitleColorForState(UIColor.whiteColor(), UIControl.UIControlStateNormal);
			button.setBackgroundImageForState(btn_bg, UIControl.UIControlStateNormal);
			button.setBackgroundImageForState(btn_bg2, UIControl.UIControlStateHighlighted);
			
			button.titleLabel().setFont(UIFont.boldSystemFontOfSize(25));
			
			button.setFrame(CGRectMake(padding * 2, bg_height, 
					        bg_width - padding * 4, btn_height)); 
			
			final int index = buttonIndex;
			button.addTargetActionForControlEvents(this, new NSSelector() {
				@Override
				public void perform(NSObject receiver, NSObject params) {
					dismissWithClickedButtonIndexAnimated(index, true);
				}
			}, UIControl.UIControlEventTouchUpInside);
			
			bgview.addSubview(button);
			
			bg_height += btn_height + padding;			
		}
		
		if(bg_height < bg_min_height) bg_height = bg_min_height;
		bgview.setFrame(CGRectMake(0, bounds.size.height - bg_height, bg_width, bg_height));
		
		window.addSubview(this);
		
		if (mDelegate != null) {
			mDelegate.didPresentActionSheet(this);
		}
	}
	
	public void dismissWithClickedButtonIndexAnimated(int buttonIndex, boolean animated) {
		if (mDelegate != null) {
			mDelegate.actionSheetClickButtonAtIndex(this, buttonIndex);
		}
		
		removeFromSuperview();
		
		if (mDelegate != null) {
			mDelegate.actionSheetDidDismissWithButtonIndex(this, buttonIndex);
		}
	}
	
}
